﻿using aier.business.logic;
using aier.dataaccess.model;
using log4net;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost.Controllers
{
    [RoutePrefix("api/Contact")]
    public class ContactController : ApiController
    {
        public ContactBll contactBll { get; set; }
        public ILog log { get; set; }
        /// <summary>
        /// 工具类
        /// </summary>
        public ApiTool apiTool { get; set; }

        [HttpPost]
        [Authorize]
        [ActionName("AddContact")]
        public IHttpActionResult AddContact(ContactInfo contactInfo)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                contactInfo.userid = new Guid(userInfo.userid.ToString());
                if (contactInfo != null)
                {
                    return Ok(contactBll.AddContact(contactInfo));
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }

        }

        /// <summary>
        /// 根据联系人id查询联系人
        /// </summary>
        /// <param name="contactId"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QryContact")]
        public IHttpActionResult QryContact(Guid contactId)
        {
            try
            {
                ContactView  contactView = contactBll.QryContact(contactId);
                return Ok(contactView);

            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }



        /// <summary>
        /// 根据姓名、负责人查询联系人
        /// </summary>
        /// <param name="name">姓名</param>
        /// <param name="ownerid">负责人</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("QryContactListByName")]
        public IHttpActionResult QryContactListByNameAndOwnerId(string name)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                Guid ownerid = new Guid(userInfo.userid.ToString());
                IList countactList = contactBll.QryContactListByNameAndOwnerId(name, ownerid).ToList();
                return Ok(countactList);

            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }

        /// <summary>
        /// 根据区域和负责人查询联系人
        /// </summary>
        /// <param name="new_districtandcounty">区域</param>
        /// <param name="new_liveness">活跃度</param>
        /// <param name="ownerid">负责人</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("QryContactList")]
        public IHttpActionResult QryContactList(Guid new_districtandcounty, int? new_liveness)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                Guid ownerid = new Guid(userInfo.userid.ToString());
                IList countactList = contactBll.QryContactList(new_districtandcounty, new_liveness, ownerid).ToList();
                return Ok(countactList);

            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }



        /// <summary>
        /// 根据负责人查询区域下拉列表
        /// </summary>
        /// <param name="ownerid">负责人</param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("QryDistrictandcountyList")]
        public IHttpActionResult QryDistrictandcountyList( int? type)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                Guid ownerid = new Guid(userInfo.userid.ToString());
                IList countactList = contactBll.QryDistrictandcountyList(ownerid, type).ToList();
                return Ok(countactList);

            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }

        }

        /// <summary>
        /// 更新联系人
        /// </summary>
        /// <param name="contactInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("UpdateContact")]
        public IHttpActionResult UpdateContact(ContactInfo contactInfo)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                contactInfo.userid = new Guid(userInfo.userid.ToString());
                if (contactInfo != null)
                {
                    return Ok(contactBll.ModContact(contactInfo));
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }

        }

        /// <summary>
        /// 删除联系人
        /// </summary>
        /// <param name="contactId">联系人ID</param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult Delete(Guid contactId)
        {
            try
            {
                return Ok(contactBll.DelContact(contactId));
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }

        }
    }
}
